jQuery.noConflict();
jQuery(document).ready(function() {    
    jQuery(":checkbox").click(function(){
        
        var id = jQuery(this).attr('id');
        
        if (jQuery('#'+id).is(':checked')) {
            jQuery("#idCheck").attr('value',id);
        }else{
            jQuery("#idCheck").attr('value','');
        }
    });
    jQuery("#fixCategories").live("click", function()
    {
       var id       = jQuery("#idCheck").val();
       var csrf     = jQuery("#csrf_babybean").val();
       if(id == '')
       {
         alert('Vui lòng chọn một dòng hoặc bỏ tích và chọn lại');
       }else{
            jQuery.ajax({
               type: "POST",
               url:  jQuery("#baseUrl").val() + 'administrator/categories/fixcategories.html',
               data: ({ids: id, csrf_babybean: csrf}),
               beforeSend: function()
               {
                    jQuery('#boxTool').html('Xin chờ....');
               },
               success: function(html)
               {
                    jQuery("#boxTool").show('slow');
                    jQuery("#boxTool").html(html);
               }
            });
       }
    });
    jQuery("#fixAjaxCategories").live("click", function()
    {
        var id      = jQuery("#id").val();
        var title   = jQuery("#title").val();
        var order   = jQuery("#order").val();
        var csrf    = jQuery("#csrf_babybean").val();
        jQuery.ajax({
           type: "POST",
           url:  jQuery("#baseUrl").val() + 'administrator/categories/fixajaxcategories.html',
           data: ({id: id, title: title, order: order, csrf_babybean: csrf}),
           beforeSend: function()
           {
                jQuery('#boxTool').fadeOut('slow');
           },
           success: function(html)
           {
                window.location.href = window.location.href;
           }
        });
    });
    jQuery("#addProduct").live("click", function()
    {
        jQuery("#control").attr("value", 1);
    });
    jQuery("#fixProduct").live("click", function()
    {
        jQuery("#control").attr("value", 2);
    });
    jQuery("#deleteProduct").live("click", function()
    {
        jQuery("#control").attr("value", 3);
        jQuery("#delete").attr('value', '1');
    });
    jQuery("#removeapp").click(function()
    {
        jQuery("#control").attr("value", 4);
    });
    jQuery("#addFacebook").click(function()
    {
        jQuery("#control").attr("value", 5);
    });
    jQuery("#phanphoi").click(function()
    {
        jQuery("#control").attr("value", 6);
    });
    jQuery("#deleteCategories").live("click", function()
    {
        jQuery("#delete").attr('value', '1');
        var csrf    = jQuery("#csrf_babybean").val();
        jQuery.ajax({
           type: "POST",
           url:  jQuery("#baseUrl").val() + 'administrator/categories/index.html',
           data: ({ids: id, csrf_babybean: csrf}),
           beforeSend: function()
           {
                jQuery('#boxTool').html('Xin chờ....');
           },
           success: function(html)
           {
                jQuery("#boxTool").html(html);
           }
        });
    });
    jQuery("#deletePartner").live("click", function()
    {
        jQuery("#delete").attr('value', '1');
        var csrf    = jQuery("#csrf_babybean").val();
        jQuery.ajax({
           type: "POST",
           url:  jQuery("#baseUrl").val() + 'administrator/user/partner.html',
           data: ({ids: id, csrf_babybean: csrf}),
           beforeSend: function()
           {
                jQuery('#boxTool').html('Xin chờ....');
           },
           success: function(html)
           {
                jQuery("#boxTool").html(html);
           }
        });
    });
    jQuery(".addFOC").live("click", function()
    {
        var id      = this.id;
        var csrf    = jQuery("#csrf_babybean").val();
        jQuery.ajax({
           type: "POST",
           url:  jQuery("#baseUrl").val() + 'administrator/orther/formorther.html',
           data: ({id: id, csrf_babybean: csrf}),
           beforeSend: function()
           {
                jQuery("#addFOC_"+id).html('Xin chờ....');
           },
           success: function(html)
           {
                jQuery("#addFOC_"+id).css('height','465px');
                jQuery("#addFOC_"+id).html(html);
                                
           }
        });
        
    });
    jQuery(".manageForm").click(function()
    {
        var id = this.id;
        jQuery.ajax({
            type: "POST",
           url:  jQuery("#baseUrl").val() + 'administrator/orther/manageform.html',
           data: ({id: id}),
           beforeSend: function()
           {
                jQuery("#addFOC_"+id).html('Xin chờ....');
           },
           success: function(html)
           {
                jQuery("#addFOC_"+id).css('height','auto');
                jQuery("#addFOC_"+id).html(html);
                                
           }
        });
        
    });
    jQuery("#typeComponent").change(function()
    {
        var id = this.value;
        if(id == 4)
        {
           jQuery("#addFormCustom").show(); 
        }
        
    });
    
       
    jQuery(".runDelParent").click(function()
    {
       if (confirm('Bạn chắc chắn xóa thành phần gốc cha này ? \nNếu bạn xóa thành phần gốc cha này thì các thành phần con sẽ bị xóa theo.\nNếu bạn đồng ý vui lòng bấm ok.')) {
            var id      = this.id;
            var csrf    = jQuery("#csrf_babybean").val();
            jQuery.ajax({
               type: "POST",
               url: jQuery("#baseUrl").val() + 'administrator/orther/deleteparent.html',
               data: ({id: id, csrf_babybean: csrf}),
               success: function(data)
               {
                    if(data == 1)
                    {
                        alert('Đây là thành phần mặc định bạn không thể thực hiện lệnh xóa với thành phần này');
                    }else{
                        window.location.href = window.location.href;
                    }
               } 
            });
        } 
    });
    
});

function runDeleteKhuyenmai(id)
{
    if (confirm('Bạn chắc chắn xóa chiến dịch khuyến mãi này ? ')) {
            var csrf    = jQuery("#csrf_babybean").val();
            jQuery.ajax({
               type: "POST",
               url: jQuery("#baseUrl").val() + 'administrator/user/delcampaign.html',
               data: ({id: id, csrf_babybean: csrf}),
               success: function(data)
               {
                    window.location.href = window.location.href;
               } 
            });
        }
}
function runUpdateKhuyenMai(id,i)
{
    var name_km                 = jQuery("#name_km_"+i).val();
    var key                     = jQuery("#key").val();
    var typeComponent           = jQuery("#typeComponent_"+i).val();
    var dieukien                = jQuery("#dieukien_"+i).val();
    var content                 = jQuery("#content_"+i).val();
    var start_time              = jQuery("#start_time_"+i).val();
    var end_time                = jQuery("#end_time_"+i).val();
    var textContent             = CKEDITOR.instances['textContent_'+i].getData();
    var csrf                    = jQuery("#csrf_babybean").val();
    if(jQuery("#stauts_"+i).is(':checked'))
    {
        var status = 1;
    }else{
        var status = 3;
    }
    if(jQuery("#popup_"+i).is(':checked'))
    {
        var popup = 1;
    }else{
        var popup = 0;
    }

    jQuery.ajax({
               type: "POST",
               url: jQuery("#baseUrl").val() + 'administrator/user/ajaxcampaign.html',
               data: ({key: key, id: id, name_km: name_km, typeComponent: typeComponent,dieukien: dieukien,content: content,start_time: start_time,end_time: end_time,textContent: textContent,status: status,popup: popup,csrf_babybean: csrf}),
               success: function(data)
               {
                if(data == 200)
                {
                    window.location.href = window.location.href;
                }
                    
               } 
            });

}

function fixPartner()
{
    var id       = jQuery("#idCheck").val();
    var csrf     = jQuery("#csrf_babybean").val();
    window.location.href = jQuery("#baseUrl").val() + 'administrator/user/fixpartner.html?ids='+id;
   
}
function addHtmlPartner()
{
    var csrf    = jQuery("#csrf_babybean").val();
    jQuery.ajax({
       type: "POST",
       url:  jQuery("#baseUrl").val() + 'administrator/user/addhtmlpartner.html',
       beforeSend: function()
       {
            jQuery("#boxTool").html('Xin chờ ... ');
       },
       data: ({csrf_babybean: csrf}),
       success: function(html)
       {
         jQuery("#boxTool").fadeIn('slow');
         jQuery("#boxTool").html(html);
       }
    });
}
function addHtmlCategories(){
    var csrf    = jQuery("#csrf_babybean").val();
    jQuery.ajax({
       type: "POST",
       url:  jQuery("#baseUrl").val() + 'administrator/categories/addhtmlcategories.html',
       beforeSend: function()
       {
            jQuery("#boxTool").html('Xin chờ ... ');
       },
       data: ({csrf_babybean: csrf}),
       success: function(html)
       {
         jQuery("#boxTool").fadeIn('slow');
         jQuery("#boxTool").html(html);
       }
    });
}

function addCategories(){
    var title   = jQuery("#title").val();
    var order   = jQuery("#order").val();
    var csrf    = jQuery("#csrf_babybean").val();
    if(title == '')
    {
        jQuery('#boxTool').fadeIn('slow');
        jQuery('#boxTool').html('<div id="errors" class="notibar msgalert" style="height: 50px"><a class="close"></a><p>Bạn vui lòng điền tên của danh mục</p></div><script type="text/javascript">jQuery(".close").click(function() { jQuery("#errors").fadeOut("slow"); }); </script>');
        return true;
    }
    jQuery.ajax({
        cache: false,
       type: "POST",
       url:  jQuery("#baseUrl").val() + 'administrator/categories/addcategories.html',
       data: ({title: title, order: order, csrf_babybean: csrf}),
       beforeSend: function()
       {
            jQuery('#boxTool').fadeOut('slow');
       },
       success: function(html)
       {
            //jQuery("#Categrories").html(html);
            if(html != '')
            {
                jQuery('#boxTool').fadeIn('slow');
                jQuery('#boxTool').html('<div id="errors" class="notibar msgalert" style="height: 50px"><a class="close"></a><p>Bạn vui lòng điền tên của danh mục</p></div><script type="text/javascript">jQuery(".close").click(function() { jQuery("#errors").fadeOut("slow"); }); </script>');
            }else{
                window.location = jQuery("#baseUrl").val() + 'administrator/categories/index/';
            }
            
       }
    });
}

function runDeleteTextEditor(id,type)
{
if (confirm('Bạn chắc chắn xóa thành phần này ? ')) {
            var csrf    = jQuery("#csrf_babybean").val();
            jQuery.ajax({
               type: "POST",
               url: jQuery("#baseUrl").val() + 'administrator/orther/deletecomponent.html',
               data: ({id: id, type: type, csrf_babybean: csrf}),
               success: function(data)
               {
                    if(data == 1)
                    {
                        alert('Đây là thành phần mặc định bạn không thể thực hiện lệnh xóa với thành phần này');
                    }else{
                        window.location.href = window.location.href;
                    }
               } 
            });
        }
}

function runUpdate(id,type)
{
    if(type == 2)
    {
        var textContent = jQuery("#textContent_"+id).val();
        var textTitle = jQuery("#textTitle_"+id).val();
    }else if(type == 1)
    {
        var textContent = CKEDITOR.instances['textEditorContent_'+id].getData();
        var textTitle = jQuery("#textEditorTitle_"+id).val();
    }
    var csrf    = jQuery("#csrf_babybean").val();

    jQuery.ajax({
           type: "POST",
           url: jQuery("#baseUrl").val() + 'administrator/orther/updatecomponent.html',
           data: ({id: id, type: type, textTitle: textTitle, textContent: textContent, csrf_babybean: csrf}),
           success: function(data)
           {
            window.location.href = window.location.href;
           } 
        });
}
function AddFileInput()
{
    var tdFileInputs = document.getElementById('tdFileInputs');
    var id1 = 'f'+(new Date()).getTime();
    var div=document.createElement('div');
    div.setAttribute('id',id1);
    tdFileInputs.appendChild(div);
    
    var id ='upfile[]';
    var file = document.createElement('input');
    file.setAttribute('id',id);
    file.setAttribute('name',id);
    file.setAttribute('type','file');
    file.setAttribute('style','width: 400px');
    div.appendChild(file);
    
    var a = document.createElement('a');
    a.setAttribute('id','remove_' + id1);
    a.setAttribute('class','removelnk');
    a.innerHTML = "Xóa<br>";
    a.onclick = RemoveFileInput;
    div.appendChild(a);
}
 
function RemoveFileInput(e)
{
    var Event = e ? e : window.event; 
    var obj = Event.target ? Event.target : window.event.srcElement; 
    var tdFileInputs = document.getElementById('tdFileInputs');
    var divId = obj.id.replace('remove_','');
    var div = document.getElementById(divId);
    tdFileInputs.removeChild(div);
}
function checkHaveFile()
        {
            var msg=document.getElementById('spanMsg');
            msg.innerHTML='';
            var Inputs = document.getElementById('tdFileInputs').getElementsByTagName('input');
            for(var n = 0; n < Inputs.length && Inputs[n].type == 'file'; ++n)
            {
                if(!(Inputs[n].value == null || Inputs[n].value.length == 0))
                    return true;
            }
            //msg.innerHTML="Bạn vui lòng chọn ít nhất 1 ảnh để tải lên???";
            return false;
        }
function deleImgArray(img,id,i)
{
    var img     = img;
    var id      = id;
    var csrf    = jQuery("#csrf_babybean").val();
    if (confirm('Bạn chắc chắn xóa ảnh này ? ')) {jQuery.ajax({
        type: "POST",
        url: jQuery("#baseUrl").val() + 'administrator/product/delimginarray.html',
        data: ({img: img, id: id, csrf_babybean: csrf}),
        success: function(data)
        {
            jQuery("#rmv_"+id+i).fadeOut();
        }
    });}
}
function exportToExcel()
{
    var data='<table border="1">'+jQuery("#Categrories").html().replace(/<a\/?[^>]+>/gi, '')+'</table>';
    jQuery('body').prepend("<form method='post' action="+'http://sieuapp.com/babybean/exportexcel.php'+" style='display:none' id='Categroriess'><input type='text' name='tableData' value='"+data+"' ></form>");
    
    jQuery('#Categroriess').submit().remove();
    setTimeout(function(){closeIframe();}, 5000);
    return false;
}
function exportToDoc()
{
    var data='<table border="1" align="center">'+jQuery("#Categrories").html().replace(/<a\/?[^>]+>/gi, '')+'</table>';
    jQuery('body').prepend("<form method='post' action="+'http://sieuapp.com/babybean/exportdoc.php'+" style='display:none' id='Categroriess'><input type='text' name='tableData' value='"+data+"' ></form>");
    jQuery('#Categroriess').submit().remove();
    setTimeout(function(){closeIframe();}, 5000);
    return false;
}


function OpenPop(popUrl, heights, widths) {
			var popUrl = popUrl;
            window.scrollTo(0, 0);
            var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;
            var height = document.documentElement.clientHeight + document.documentElement.scrollTop;
            var layer = document.createElement("div");
            layer.style.zIndex = 2;
            layer.id = "layer";
            layer.style.position = "absolute";
            layer.style.top = "0px";
            layer.style.left = "0px";
            layer.style.height = "1000px";//document.documentElement.scrollHeight + "px";
            layer.style.width = width + "px";
            layer.style.backgroundColor = "black";
            layer.style.opacity = "0.75";
            layer.style.filter += ("progid:DXImageTransform.Microsoft.Alpha(opacity=75)");
            document.body.style.position = "static";
            document.body.appendChild(layer);
            var iframe = document.createElement("iframe");
           
            iframe.name = "Export excel by nhatnv";
            iframe.id = "popup";
            iframe.src = popUrl;
            iframe.style.height = heights + "px";
            iframe.style.width = widths + "px";
            iframe.style.position = "fixed";
            iframe.style.zIndex = 3;
            iframe.style.backgroundColor = "white";
            iframe.frameborder = "0";
            iframe.style.top = ((height + document.documentElement.scrollTop) / 2) - (heights / 2) + "px";
            iframe.style.left = (width / 2) - (widths / 2) + "px";
            document.body.appendChild(iframe);
        }
function ClosePop() {
    var layer = document.getElementById("layer");
    var iframe = document.getElementById("popup");
    document.body.removeChild(layer); // remove layer
    document.body.removeChild(iframe); // remove div
}
function closeIframe() {
    window.parent.ClosePop();
    
}